﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.Remoting.Contexts;
using mscook.core.domain;
using mscook.core.repositories;



namespace mscook.core.components
{
    public class CurrentUser
    {
        public CurrentUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                //HttpContext.Current.Response.RedirectLocation="LoginT.aspx";
                throw new WeatException("用户未登录");//必须要用户Form验证后才能使用CurrentUser

            var cache = new WebCache();
            if (HttpContext.Current.Session["currentuser"] == null)
            {
                //var menuRepo = UnityHelper.UnityToT<ISystemMenuRepository>();
                //var userRepo = UnityHelper.UnityToT<ISystemUserRepository>();
                //var buttonRepo = UnityHelper.UnityToT<ISystemButtonRepository>();
                //var companyRepo = UnityHelper.UnityToT<ISystemCompanyRepository>();
                var loginRepo = UnityHelper.UnityToT<IRepository<LoginUser>>();

                //string CompanyID = ((System.Web.Security.FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData.ToString();
                string loginId = HttpContext.Current.User.Identity.Name;

                UserInfo = loginRepo.Get(loginId);

               
                cache.SetSessionCache("currentuser", this);
            }
            else
            {
                var u = (CurrentUser)cache.GetSessionCache("currentuser");
                UserInfo = u.UserInfo;
            }
        }

        //用户信息
        public LoginUser UserInfo{ get; set; }


    }
}
